//-----------------------------------------------------------------------
//  This file is part of: 
//  Visual Studio Team Edition for Database Professionals
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//  
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

namespace Demo
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Text;
	using System.Windows.Forms;
	using System.Configuration;
	using Microsoft.VisualStudio.TeamSystem.Data.Generators;
	using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator;

    public partial class RegexStringSampler : Form
    {
        private RegexString rs;
        private GeneratorInit gi;

        public RegexStringSampler()
        {
            InitializeComponent();
            rs = null;
            gi = null;

            cbExpressions.Items.Clear();

            foreach (string key in ConfigurationManager.AppSettings)
            {
                int i = cbExpressions.Items.Add(key);
            }

            if (cbExpressions.Items.Count > 0)
            {
                cbExpressions.SelectedIndex = 0;
                txtExpr.Text = ConfigurationManager.AppSettings[cbExpressions.SelectedItem.ToString()];
            }
        }

        private void txtExpr_TextChanged(object sender, EventArgs e)
        {
            try
            {
                gi = new GeneratorInit(null);

                rs = new RegexString();
                rs.Collation = "latin1_general_cs_as";
                rs.ColumnName = "column";
                rs.Constraints = new System.Collections.ObjectModel.Collection<string>();
                rs.DefaultLocaleId = 1033;
                rs.DefaultValue = "defaultvalue";
                rs.MaximumLength = 100;
                rs.MaxLength = 100;
                rs.Nullable = false;
                rs.OutputKey = "outputkey";
                rs.PercentageNull = 0;
                rs.Precision = 0;
                rs.RowsToInsert = 20;
                rs.Scale = 0;
                rs.SchemaName = "schema";
                rs.Seed = 5;
                rs.TableName = "table";
                rs.TargetType = "string";
                rs.Unique = false;

                rs.Expression = txtExpr.Text;
                rs.Initialize(gi);

                for (int i = 0; i < rs.RowsToInsert; i++)
                {
                    rs.GenerateNextValues();
                    if (i == 0)
                        txtResult.Text = rs.Result;
                    else
                        txtResult.Text += "\r\n" + rs.Result;
                }
            }
            catch (System.Exception ex)
            {
                txtResult.Text = ex.Message;
            }
            finally
            {
                rs = null;
                gi = null;
            }
        }

        private void cbExpressions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbExpressions.SelectedIndex != -1)
            {
                txtExpr.Text = ConfigurationManager.AppSettings[cbExpressions.SelectedItem.ToString()];
            }
        }
    }
}